//JavaScript for GadgetFreak


/* GLOBAL VARIABLES ----------------------------------------------------------------------------------------- */
var g_variable = 0;


/* jQuery UI INITIALIZATION --------------------------------------------------------------------------------- */

$(document).ready(function() {
	$("#dialog_registration").dialog({
		autoOpen: false,
		height: 550,
		width: 420,
		modal: true,
		buttons: {
			"Register": function() {
				var bValid = true;

				var inputs = document.getElementById('form_registration').elements;
			    for(var k = 0; k < inputs.length; k++) {
			    	if (inputs[k].value == "") {
						bValid = false;
						break;
					}
			    }
						
				if ($("#registration_email")[0].value != $("#registration_email_repeated")[0].value)
					bValid = false;
				else if ($("#registration_password")[0].value != $("#registration_password_repeated")[0].value)
					bValid = false;
				
				
				if ( bValid ) {
					$("#registration_format_err")[0].style.visibility = "hidden";
					$("#form_registration")[0].submit();
					$(this).dialog( "close" );
				} else {
					$("#registration_format_err")[0].style.visibility = "visible";
				}
			},
			Cancel: function() {
				$(this).dialog( "close" );
			}
		},
		close: function() {
			$(this).dialog( "close" );
		}
	});
});

$(function() {
	$("#tabs").tabs();
});


/* main FUNCTIONS ------------------------------------------------------------------------------------------- */

function onPrijavi() {
	var inputs = document.getElementById('form_login').elements;
    for(var k = 0; k < inputs.length; k++) {
    	if (inputs[k].value == "") {
    		$("#format_err")[0].style.visibility = "visible";
			break;
		}
    }
}

function initRegistration() {
	$("#dialog_registration").dialog( "open" );
}

function checkUsername() {

	var dom_err = document.getElementById("format_err_username").style;
	var expr = /^[a-zA-Z_0-9.]{3,20}$/
	if (document.getElementById("registration_username").value.search(expr) == -1
			&& document.getElementById("registration_username").value != "") {
		dom_err.visibility = "visible";	
	} else {
		dom_err.visibility = "hidden";
	}
	updateRegisterSubmitButton();
}
function checkName() {

	var dom_err = document.getElementById("format_err_name").style;
	var expr = /^[a-zA-Z ]{2,30}$/
	if (document.getElementById("name").value.search(expr) == -1
			&& document.getElementById("name").value != "") {
		dom_err.visibility = "visible";	
	} else {
		dom_err.visibility = "hidden";
	}
	updateRegisterSubmitButton();
}
function checkSurname() {

	var dom_err = document.getElementById("format_err_surname").style;
	var expr = /^[a-zA-Z_]{2,30}$/
	if (document.getElementById("surname").value.search(expr) == -1
			&& document.getElementById("surname").value != "") {
		dom_err.visibility = "visible";	
	} else {
		dom_err.visibility = "hidden";
	}
	updateRegisterSubmitButton();
}
function checkRegistrationEmail() {

	var dom_err = document.getElementById("format_err_registration_email").style;
	var expr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,253}\.[a-zA-Z]{2,4}$/
	if (document.getElementById("registration_email").value.search(expr) == -1
			&& document.getElementById("registration_email").value != "") {
		dom_err.visibility = "visible";	
	} else {
		dom_err.visibility = "hidden";
	}
	updateRegisterSubmitButton();
}
function checkRegistrationPassword() {

	var dom_err = document.getElementById("format_err_registration_password").style;
	var expr = /^[a-zA-Z0-9-@#$%^&+=]{4,24}$/
	if (document.getElementById("registration_password").value.search(expr) == -1
			&& document.getElementById("registration_password").value != "") {
		dom_err.visibility = "visible";

	} else {
		dom_err.visibility = "hidden";
	}
	updateRegisterSubmitButton();
}

function updateRegisterSubmitButton() {
	
	var valid = ($("#format_err_username")[0].style.visibility == "hidden") &&
				($("#format_err_name")[0].style.visibility == "hidden") &&
				($("#format_err_surname")[0].style.visibility == "hidden") &&
				($("#format_err_registration_email")[0].style.visibility == "hidden") &&
				($("#format_err_registration_password")[0].style.visibility == "hidden");
		
	if (valid)
		$('button:eq(0)',$('#dialog_registration').dialog.buttons).button('enable');
	else
		$('button:eq(0)',$('#dialog_registration').dialog.buttons).button('disable');
		
}

function onDodajNapravo() {
	$('#div_nova_naprava').show();
	$('#div_lastnosti_naprave').hide();
	$('#div_slike_naprave').hide();
	$('#div_preizkus_naprave').hide();
	$('#gadget_1').hide();
}
function onDodajLastnostiNaprave() {
	$('#div_nova_naprava').hide();
	$('#div_lastnosti_naprave').show();
	$('#div_slike_naprave').hide();
	$('#div_preizkus_naprave').hide();
	$('#gadget_1').hide();
}
function onDodajSlikeNaprave() {
	$('#div_nova_naprava').hide();
	$('#div_lastnosti_naprave').hide();
	$('#div_slike_naprave').show();
	$('#div_preizkus_naprave').hide();
	$('#gadget_1').hide();
}
function onDodajPreizkusNaprave() {
	$('#div_nova_naprava').hide();
	$('#div_lastnosti_naprave').hide();
	$('#div_slike_naprave').hide();
	$('#div_preizkus_naprave').show();
	$('#gadget_1').hide();
}